package 
{
	import app.AppModel;
	import app.AppSettings;
	import app.AppView;
	import builder.AppBuilderXML;
	import builder.IAppBuilder;
	import builder.MakeXML;
	import builder.TestAppBuilder;
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.geom.Point;
	import test.MakeFurnXml;
	import test.TestBezier;
	import utils.ErrorsCollector;

	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	[Frame(factoryClass="Preloader")]
	public class Main extends Sprite 
	{
		private var _model:AppModel;
		private var _builder:IAppBuilder;
		private var _stagePrevWidth:Number = 0;
		private var _stagePrevHeight:Number = 0;
		private var _app:DisplayObject;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			trace("Main started!");
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			//addChild(new TestBezier());
			//return;
			
			
			/*var currentDomain:String = Domain(this);
			trace("Domain: " + currentDomain);
			if ((currentDomain != "zz.mu"))
			{
				var siteLockSprite:Sprite = new PageSiteLock();
				this.addChild(siteLockSprite);
				return;
			}
			*/
			
			/*var p:Point = new Point(Math.cos(Math.PI * 0.25), Math.sin(Math.PI * 0.25));
			p.normalize(50);
			trace("shif x: " + p.x + " y: " + p.y);*/
			//addChild(new MakeFurnXml());
		//	return;
			
			var bg:Sprite = new MainBgMov();
		//	bg.x = (AppSettings.APP_FULL_WIDTH - bg.width) * 0.5;
			//bg.y = (AppSettings.APP_FULL_HEIGHT - bg.height) * 0.5;
			addChild(bg);
			
			_model = new AppModel();
			_model.addEventListener(Event.CHANGE, modelChanged);
			
			//_contentLoader = new ContentLoader(_model, this.stage);
			_builder = new AppBuilderXML(_model, stage);
			_builder.start();
		}
		
		private function Domain(root:Sprite):String 
		{
			trace("site: " + root.loaderInfo.url);
			var currentDomain:String = root.loaderInfo.url.split("/")[2];
			var fqdn:Array = currentDomain.split(".");
			var rdi:int = 1;
			var tli:int = 2; 
			if (fqdn.length == 2) {
				rdi--;
				tli--;
			}

			return fqdn[rdi] + "." + fqdn[tli];
		}

		
		private function modelChanged(e:Event):void 
		{
			if (_model.inited)
			{
				_model.removeEventListener(Event.CHANGE, modelChanged);
				_app = new AppView(_model);
				_app.x = 235;
				_app.y = 5;
				addChild(_app);
				_builder.destroy();
				
				/*var shape:Shape = new Shape();
				addChild(shape);
				shape.graphics.beginFill(0xFF0000);
				shape.graphics.drawCircle(0, 0, 20);
				shape.graphics.endFill();
				*/
				
				//addEventListener(Event.ENTER_FRAME, handleEnterFrame);
			}
		}
		
		private function handleEnterFrame(e:Event):void 
		{
			trace("Stage width: " + stage.width);
			if ((stage.width != _stagePrevWidth) || (stage.height != _stagePrevHeight))
			{
				trace("move");
				_stagePrevWidth = stage.width;
				_stagePrevHeight = stage.height;
				this.x = (_stagePrevWidth - AppSettings.APP_WIDTH) * 0.5;
				this.y = (_stagePrevHeight - AppSettings.APP_HEIGHT) * 0.5;
			}
		}

	}

}